package com.frank.yinyuetai;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.frank.yinyuetai.Bean.VideoBean;
import com.frank.yinyuetai.Fragment.DescribeFragment;
import com.frank.yinyuetai.Fragment.MVRelativeFragment;
import com.frank.yinyuetai.Util.URLProviderUtil;
import com.google.gson.Gson;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import butterknife.Bind;
import butterknife.ButterKnife;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/8/17 0017.
 */
public class MVDetailActivity extends AppCompatActivity implements View.OnClickListener {
    @Bind(R.id.jc_mv_player)
    JCVideoPlayerStandard playerStandard;
    @Bind(R.id.mv_comment)
    ImageView comment;
    @Bind(R.id.mv_describe)
    ImageView describe;
    @Bind(R.id.relative_mv)
    ImageView relative;

    private int id;
    private DescribeFragment describeFragment;
    private MVRelativeFragment relativeFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mv_detail_layout);
        ButterKnife.bind(this);
        id = getIntent().getExtras().getInt("id");
        comment.setOnClickListener(this);
        describe.setOnClickListener(this);
        relative.setOnClickListener(this);
        describe.setImageResource(R.drawable.player_mv_p);
        comment.setImageResource(R.drawable.player_comment);
        relative.setImageResource(R.drawable.player_relative_mv);
        requestData();

    }

    private void requestData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpUtils.getInstance()
                        .get(URLProviderUtil.getRelativeVideoListUrl(id))
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                Gson gson = new Gson();
                                VideoBean videoBean = gson.fromJson(s, VideoBean.class);
                                Log.i("FHJKHJK", videoBean.getTitle());
                                describeFragment = DescribeFragment.nesInstance(videoBean);
                                relativeFragment = MVRelativeFragment.newInstance(videoBean);
                                playerStandard.setUp(videoBean.getShdUrl(), videoBean.getTitle());
                                playerStandard.startButton.performClick();
                                setFragment(describeFragment);
                            }

                        });

            }
        }).start();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mv_describe:
                describe.setImageResource(R.drawable.player_mv_p);
                comment.setImageResource(R.drawable.player_comment);
                relative.setImageResource(R.drawable.player_relative_mv);
                setFragment(describeFragment);
                break;
            case R.id.mv_comment:
                comment.setImageResource(R.drawable.player_comment_p);
                relative.setImageResource(R.drawable.player_relative_mv);
                describe.setImageResource(R.drawable.player_mv);
                break;
            case R.id.relative_mv:
                relative.setImageResource(R.drawable.player_relative_mv_p);
                describe.setImageResource(R.drawable.player_mv);
                comment.setImageResource(R.drawable.player_comment);
                setFragment(relativeFragment);
                break;
        }

    }

    private void setFragment(Fragment fragment) {
        if (fragment == null) {
            return;
        }
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (fragment.isAdded() && fragment.isVisible()) {
            return;
        }
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.replace(R.id.frame_mv, fragment);
        }
        transaction.commit();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }
}
